<template>
  <div class="pagination-container">
    <el-pagination
      :background="background"
      :total="total"
      :layout="layout"
      :page-sizes="pageSizes"
      :current-page.sync="currentPage"
      :page-size.sync="pageSiz"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  name: 'Pagination',
  props: {
    total: {
      type: Number,
      required: true
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    pageSizes: {
      type: Array,
      default () {
        return [10, 20, 30, 40]
      }
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 2
    }
  },
  /**
   * 为什么需要计算属性, 因为不光要接收值，还要传值
   */
  computed: {
    currentPage: {
      get () {
        return this.page
      },
      set (val) {
        /**
         * update page这个属性, page在父组件中监听, 当子组件改变page这个属性时, 父组件的page也会改变
         */
        this.$emit('update:page', val)
      }
    },
    pageSiz: {
      get () {
        return this.limit
      },
      set (val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      this.$emit('pagination')
    },
    handleCurrentChange (val) {
      this.$emit('pagination')
    }
  }
}
</script>

<style lang="scss" scoped>
.pagination-container {
  padding: 32px 16px;
}
</style>
